<template>
  
  <div class="parbox">
        <div class="box1">
            <batch-operat-btn v-if="editable" :isView="false" :addData="true" @onAddData="onAddData">
                <template #cusBtn> </template>
            </batch-operat-btn>
            <div class="recordBox">
                <a-list-table
                :columns="columns"
                :tableData="listdata"
                :isSelecttion="false"
                :loading="loading"
                :scroll="{ y: null }"
                @onAction="onAction"
                >
                </a-list-table>
            </div>
        </div>
        <div class="box2">
            <a-button class="btn" style="marginRight:20px"  type="default"  @click="previous()">
                上一步
            </a-button>
            <a-button class="btn" style="marginRight:20px"  type="default"  @click="skip()">
                跳过
            </a-button>
            <a-button class="btn"  type="primary"  @click="nexts()">
                下一步
            </a-button>
        </div>
        <add-seller-lic-child-modal ref="addSellerLicChildModal" @onAddInfo="onAddInfo" />
    </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import addSellerLicChildModal from './addSellerLicChildModal.vue'
// import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getLicenseByPid, saveSellerLicense } from './regisLicense'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    addSellerLicChildModal,
  },
  props: {
    zzid: {
      type: Object,
    }
  },
  data() {
    return {
      footer:undefined,
      editable:true,
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '上传厂家证照',
        modalWidth: '1000px',
      },
      columns: [
        { title: '证照类型', dataIndex: 'zhengJielx', width: 80, key: 'zhengJielx', align: 'center' },
        { title: '证照号', dataIndex: 'zhengJiehm', key: 'zhengJiehm', align: 'center' },
        { title: '证照开始日期', dataIndex: 'youXiaoqs', key: 'youXiaoqs', align: 'center' },
        { title: '证照结束日期', dataIndex: 'youXiaoqz', key: 'youXiaoqz', align: 'center' },
        { title: '证照备注', dataIndex: 'zhengJiebz', key: 'zhengJiebz', align: 'center' },
        {
          title: '证照图片',
          dataIndex: 'zhengJietp',
          key: 'zhengJietp',
          align: 'center',
          scopedSlots: { customRender: 'img' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 100,
          key: 'operation',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      listdata: [],
      listKey: 0,
      pid: '',
      sysSeq: 0,
      loadings: false,
      isSubmit:false
    }
  },
  computed: {},
  mounted() {},
  watch: {
    zzid: {
        handler: function (val, oldVal) {
            console.log(this.zzid);
            this.pid=this.zzid.pid
            this.sysSeq=this.zzid.sysSeq
        },
        deep: true,
        immediate:true
    }
  },
  methods: {
    
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    //
    onAddData() {
      this.$refs.addSellerLicChildModal.addModal(this.pid)
    },
    // 子modal提交过来的信息
    onAddInfo(form) {
      let existed = false
      this.listdata.find((item) => {
        if (form.zhengJiehm == item.zhengJiehm) {
          existed = true
        }
      })
      if (!existed) {
        this.listdata.push({
          key: form.zhengJiehm,
          action: ['删除'],
          ...form,
        })
        this.$refs.addSellerLicChildModal.closeModal()
      } else {
        this.$message.warn({ content: '该证件号已存在' })
      }
      console.log(this.listdata)
    },
    previous(){
      this.$emit("onCancel") 
    },
    skip(){
      this.$emit("onSkip",3) 
    },
    nexts() {
      if(this.listdata.length){
      let listdata = JSON.stringify(this.listdata)
      let subData = {
        sysSeq: this.sysSeq,
        pid: this.pid,
        shengChancszz: listdata,
        shengChancszzzt: listdata === '[]' ? '0': '1'
      }
      console.log(listdata)
      if(this.isSubmit){
          this.$emit('nextStep')
      }else{
        this.saveObjInfo(subData)
      }
      
      }else{
        this.$message.warn({ content: '暂未添加任何厂家证照信息', duration: 2 })
      }
    },
    // 保存详细信息
    async saveObjInfo(val) {
      let res = await saveSellerLicense(val)
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '保存成功', duration: 2 })
        this.isSubmit=true;
        this.$emit('nextStep')
      } else {
        this.$message.error({ content: '保存失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      this.loading=true
      let res = await getLicenseByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        if (res.data.shengChancszz) {
          this.listdata = JSON.parse(res.data.shengChancszz)
        }
        this.loading=false
      }
      console.log(res)
    },
    onAction(text, record, index) {
      console.log(text, record, index)
      if (text == '删除') {
        this.listdata.splice(index, 1)
      }
    },
  },
}
</script>

<style  scoped>
.recordBox {
  width: 100%;
  min-height: 80px;
}

</style>